/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asset;

import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
public class MeshSet {

    Map<String, Mesh> submeshes;

    public MeshSet() {    
        this.submeshes = new HashMap<>();
    }

    public void addSubmesh(String name, Mesh mesh) {
        submeshes.put(name, mesh);
    }

    /**
     * Create geometries that reuse the shared mesh data as much as possible.
     * @param nodeName
     * @param material
     * @return 
     */
    public Node instantiate(String nodeName, Material material) {

        Node modelNode = new Node(nodeName);

        for (Map.Entry<String, Mesh> e : submeshes.entrySet()) {
            String name = e.getKey();
            Mesh mesh = e.getValue();
            
            // We need to clone for animation purposes, because some
            // vertex buffers are modified during the animation.
            Mesh clone = mesh.cloneForAnim();
            
            Geometry geo = new Geometry(name, clone);
            geo.setMaterial(material);
            modelNode.attachChild(geo);
        }

        return modelNode;
    }
}
